package com.hfut.manage.controller;

import com.hfut.manage.pojo.Education;
import com.hfut.manage.pojo.Result;
import com.hfut.manage.pojo.Teacher;
import com.hfut.manage.service.IEducationService;
import com.hfut.manage.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/education")
public class EducationController {

    @Autowired
    private IEducationService educationService;
    @Autowired
    private ITeacherService teacherService;

    @GetMapping
    public Result list() {
        List<Education> departmentList = educationService.list();
        return Result.success(departmentList);
    }

    @GetMapping("/name/{teacherName}")
    public Result list(@PathVariable("teacherName") String name) {
        List<Teacher> teacherList = teacherService.lambdaQuery().eq(Teacher::getName,name).list();
        List<Integer> teacherIds = teacherList.stream()
                .map(Teacher::getId)
                .collect(Collectors.toList());
        List<Education> educationList = educationService.lambdaQuery()
                .in(Education::getTeacherId, teacherIds)
                .list();
        return Result.success(educationList);
    }


    @GetMapping("/id/{id}")
    public Result getByTeacherId(@PathVariable("id") Long Id) {
        System.out.println("<UNK>id<UNK>====================\n" + Id);
        Education educationList = educationService.lambdaQuery()
                .eq(Education::getId, Id)
                .one();

        return Result.success(educationList);
    }

    @PutMapping
    public Result updata(@RequestBody Education education) {
        System.out.println("==============================="+education);
        boolean success = educationService.updateById(education);
        if (!success) {
            return Result.error(500, "新增教育经历失败");
        }
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Education education) {
        boolean success = educationService.save(education);
        if (!success) {
            return Result.error(500, "新增教育经历失败");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Long id) {
        boolean success = educationService.removeById(id);
        if (!success) {
            return Result.error(500, "删除教育经历失败");
        }
        return Result.success();
    }

}
